<?php
namespace database;

use database\DbHelper;
use utils\PathTool;
use model\DnFileInf;
use sql\SqlExec;

class pager
{	
	function read($pageIndex,$id)
	{
		$pageSize = 100;
		$pageStart = ((intval($pageIndex)-1)*$pageSize);
		
		$se = new SqlExec();
		$o = $se->read("up7_files", "*", array("f_id"=>$id));
		//子目录
		if($o == null)
		{
		    $o = $se->read("up7_folders", "*", array("f_id"=>$id));
		}
		
		$pathRoot = $o["f_pathRel"];
		$index = strlen($pathRoot);
		
		$sql = "select f_id,f_nameLoc,f_pathLoc,f_pathSvr,f_pathRel,f_blockPath,f_blockSize,f_lenLoc,f_sizeLoc,f_object_url from up7_files where f_pidRoot='$id' limit $pageStart, $pageSize";
		//
		$db = new DbHelper();
		$cmd = $db->prepare_utf8($sql);
		$ret = $db->ExecuteDataSet($cmd);
		$files = array();

		foreach($ret as $row)
		{
			$f            = new DnFileInf();
			$f->id        = PathTool::guid();
			$f->f_id      = $row["f_id"];
			$f->nameLoc   = $row["f_nameLoc"];
			$f->pathLoc   = $row["f_pathLoc"];
			$f->pathSvr   = $row["f_pathSvr"];
			$f->pathRel   = $row["f_pathRel"];			
			$f->pathRel   = substr($f->pathRel, $index);			
			$f->blockPath = $row["f_blockPath"];
			$f->blockSize = $row["f_blockSize"];
			$f->lenSvr    = $row["f_lenLoc"];
			$f->sizeSvr   = $row["f_sizeLoc"];
			$f->objectUrl = $row["f_object_url"];
			
			$f->nameLoc   = PathTool::url_encode($f->nameLoc);
			$f->pathLoc   = PathTool::urlencode_path($f->pathLoc);
			$f->pathSvr   = PathTool::urlencode_path($f->pathSvr);
			$f->pathRel   = PathTool::urlencode_path($f->pathRel);
			$f->blockPath = PathTool::urlencode_path($f->blockPath);
			//灏哱\杞崲涓�/
			$f->pathSvr   = str_replace("%5C%5C", "%2F", $f->pathSvr);
			$f->pathRel   = str_replace("%5C%5C", "%2F", $f->pathRel);
			$f->blockPath = str_replace("%5C%5C", "%2F", $f->blockPath);
			$files[] = $f;
		}
		$json = json_encode($files);
		$json = PathTool::url_decode($json);//杩樺師姹夊瓧
		return $json;
	}
}
?>